<?php
class Mhome extends Model {
        function Mhome() {
			parent::Model();
        }
        /*------------------------------------header.php部分[会员系统]----------------------------*/
        //注册会员

        //注册会员号生成
        function create_member_uid($num=1){
        	//查找到没有注册的号码
        	$sql="select uid from mc_member_ids where state=0 order by uid limit 0,$num";
            $query=$this->db->query($sql);
            //将已经注册好的state改成1
            $first_uid=$query->first_row()->uid;
            $end_uid=$query->first_row()->uid+$num;
            $sql2="update `mc_member_ids` set `state` = '1' where uid >=$first_uid and uid <$end_uid";
            $query2=$this->db->query($sql2);
            return $query->result();
        }
        
        /*------------------------------------left.php部分------------------------------------*/
		//最新公告，限制公告条数
        function lastes_news($num) {
			$sql="select * from mc_news order by news_time desc limit 0,$num";
            $query=$this->db->query($sql);
			return $query->result();
        }
        //公告里的news,新闻ID，新闻类型
        function news($news_id,$type){
        	$sql="select * from mc_news where id=$news_id and news_type=$type";
            $query=$this->db->query($sql);
			return $query->result();
        }

		//银行列表
		function banks() {
			$sql="select * from mc_banks order by border asc";
            $query=$this->db->query($sql);
			return $query->result();
		}
		//网吧列表
		function netbar(){
			$sql="select * from mc_netbar_areas";
			$query=$this->db->query($sql);
			foreach($query->result() as $row){
				$row->has_bar=$this->has_bar($row->area_id);
			}
			return $query->result_object;
		}
		//判断有无网吧
		function has_bar($area_id){
			$this->db->where('area_id',$area_id);
    		return $this->db->count_all_results('mc_netbars');
		}
		
		/*------------------------------------index.php部分[产品展示]------------------------------------*/
		//套餐分类
		function products_type($num=10,$i=1){
			$sql="select * from mc_product_type";
			$query=$this->db->query($sql);
			foreach($query->result() as $row){
				$row->i=$i++;
				$row->products=$this->products($row->id,$num);
				$row->has_products=$this->has_products($row->id);
			}
			//print_r($query->result_object);
			return $query->result_object;
		}
		//套餐展示,$num限定显示几列数据
		function products($p_type,$num,$j=1){
			$sql="select * from mc_products where p_type=$p_type order by add_time desc limit 0,$num";
			$query=$this->db->query($sql);
			foreach($query->result() as $row){
				$row->j=$j++;
			}
			return $query->result_object;	
		}
		//判断某种类型的套餐是否存在
		function has_products($p_type){
			$this->db->where('p_type',$p_type);
    		return $this->db->count_all_results('mc_products');
		}
		//热门套餐
		function hot_products($num=18,$j=50){
			$sql="select * from mc_products order by buy_times desc limit 0,$num";
			$query=$this->db->query($sql);
			return $this->addobj($query,$j);	
		}
		/*------------------------------------index.php部分[套餐购买]------------------------------*/
				
		
		/*--------------------------------Afei自定义辅助函数部分------------------------------------*/
		//向对象中添加对象，产生一个序列i[1,2,3,4][原理：套餐分类]
		function addobj($query,$j=1){
			foreach($query->result() as $row){
				$row->j=$j++;
			}
			return $query->result_object;		
		}
}//end mhome.mode
?>